﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ZombieGame
{
    public class GUITextbox : GUIItem
    {
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                Size = ZombieGame.Font1.MeasureString(_text);
            }
        }
        public string _text;
        public bool Focused;
        public int Index;
        float i;

        public GUITextbox()
            : base()
        {
            Text = "";
        }

        public override void Draw(SpriteBatch batch)
        {
            string nText = Text;
            if (Focused && (int)i % 2 == 0)
                nText = Text.Insert(Index, "|");
            if (i > 2f)
                i -= 2f;
            batch.DrawString(ZombieGame.Font1, nText, Position, Color, Rotation, new Vector2(), Scale, SpriteEffects.None, 0f);
        }

        public override void Update(GameTime time)
        {
            if (Focused)
                i += (float)time.ElapsedGameTime.TotalSeconds;
            else
                i = 0f;
            Vector2 m = new Vector2(InputHandler.CurrentMouseState.X, InputHandler.CurrentMouseState.Y);
            try
            {
                if (InputHandler.MouseLPress())
                    if (m.X > Bounds.Left && m.X < Bounds.Right && m.Y > Bounds.Top && m.Y < Bounds.Bottom)
                        if (ImageData.Count > 0)
                        {
                            Vector2 v = ScreenToClient(m);
                            if (ImageData[CurrentFrame][(int)v.X, (int)v.Y].A > 0)
                                Focused = true;
                        }
                        else
                            Focused = true;
                    else
                        Focused = false;
                if (Focused)
                    foreach (Keys k in InputHandler.CurrentKeyState.GetPressedKeys())
                        if (InputHandler.IsNewDown(k))
                        {
                            char c = 'ת';
                            bool shift = InputHandler.IsDown(Keys.LeftShift) || InputHandler.IsDown(Keys.RightShift);
                                switch (k)
                                {
                                    //Alphabet keys
                                    case Keys.A: if (shift) { c = 'A'; } else { c = 'a'; } break;
                                    case Keys.B: if (shift) { c = 'B'; } else { c = 'b'; } break;
                                    case Keys.C: if (shift) { c = 'C'; } else { c = 'c'; } break;
                                    case Keys.D: if (shift) { c = 'D'; } else { c = 'd'; } break;
                                    case Keys.E: if (shift) { c = 'E'; } else { c = 'e'; } break;
                                    case Keys.F: if (shift) { c = 'F'; } else { c = 'f'; } break;
                                    case Keys.G: if (shift) { c = 'G'; } else { c = 'g'; } break;
                                    case Keys.H: if (shift) { c = 'H'; } else { c = 'h'; } break;
                                    case Keys.I: if (shift) { c = 'I'; } else { c = 'i'; } break;
                                    case Keys.J: if (shift) { c = 'J'; } else { c = 'j'; } break;
                                    case Keys.K: if (shift) { c = 'K'; } else { c = 'k'; } break;
                                    case Keys.L: if (shift) { c = 'L'; } else { c = 'l'; } break;
                                    case Keys.M: if (shift) { c = 'M'; } else { c = 'm'; } break;
                                    case Keys.N: if (shift) { c = 'N'; } else { c = 'n'; } break;
                                    case Keys.O: if (shift) { c = 'O'; } else { c = 'o'; } break;
                                    case Keys.P: if (shift) { c = 'P'; } else { c = 'p'; } break;
                                    case Keys.Q: if (shift) { c = 'Q'; } else { c = 'q'; } break;
                                    case Keys.R: if (shift) { c = 'R'; } else { c = 'r'; } break;
                                    case Keys.S: if (shift) { c = 'S'; } else { c = 's'; } break;
                                    case Keys.T: if (shift) { c = 'T'; } else { c = 't'; } break;
                                    case Keys.U: if (shift) { c = 'U'; } else { c = 'u'; } break;
                                    case Keys.V: if (shift) { c = 'V'; } else { c = 'v'; } break;
                                    case Keys.W: if (shift) { c = 'W'; } else { c = 'w'; } break;
                                    case Keys.X: if (shift) { c = 'X'; } else { c = 'x'; } break;
                                    case Keys.Y: if (shift) { c = 'Y'; } else { c = 'y'; } break;
                                    case Keys.Z: if (shift) { c = 'Z'; } else { c = 'z'; } break;

                                    //Decimal cs
                                    case Keys.D0: if (shift) { c = ')'; } else { c = '0'; } break;
                                    case Keys.D1: if (shift) { c = '!'; } else { c = '1'; } break;
                                    case Keys.D2: if (shift) { c = '@'; } else { c = '2'; } break;
                                    case Keys.D3: if (shift) { c = '#'; } else { c = '3'; } break;
                                    case Keys.D4: if (shift) { c = '$'; } else { c = '4'; } break;
                                    case Keys.D5: if (shift) { c = '%'; } else { c = '5'; } break;
                                    case Keys.D6: if (shift) { c = '^'; } else { c = '6'; } break;
                                    case Keys.D7: if (shift) { c = '&'; } else { c = '7'; } break;
                                    case Keys.D8: if (shift) { c = '*'; } else { c = '8'; } break;
                                    case Keys.D9: if (shift) { c = '('; } else { c = '9'; } break;

                                    //Decimal numpad cs
                                    case Keys.NumPad0: c = '0'; break;
                                    case Keys.NumPad1: c = '1'; break;
                                    case Keys.NumPad2: c = '2'; break;
                                    case Keys.NumPad3: c = '3'; break;
                                    case Keys.NumPad4: c = '4'; break;
                                    case Keys.NumPad5: c = '5'; break;
                                    case Keys.NumPad6: c = '6'; break;
                                    case Keys.NumPad7: c = '7'; break;
                                    case Keys.NumPad8: c = '8'; break;
                                    case Keys.NumPad9: c = '9'; break;

                                    //Special cs
                                    case Keys.OemTilde: if (shift) { c = '~'; } else { c = '`'; } break;
                                    case Keys.OemSemicolon: if (shift) { c = ':'; } else { c = ';'; } break;
                                    case Keys.OemQuotes: if (shift) { c = '"'; } else { c = '\''; } break;
                                    case Keys.OemQuestion: if (shift) { c = '?'; } else { c = '/'; } break;
                                    case Keys.OemPlus: if (shift) { c = '+'; } else { c = '='; } break;
                                    case Keys.OemPipe: if (shift) { c = '|'; } else { c = '\\'; } break;
                                    case Keys.OemPeriod: if (shift) { c = '>'; } else { c = '.'; } break;
                                    case Keys.OemOpenBrackets: if (shift) { c = '{'; } else { c = '['; } break;
                                    case Keys.OemCloseBrackets: if (shift) { c = '}'; } else { c = ']'; } break;
                                    case Keys.OemMinus: if (shift) { c = '_'; } else { c = '-'; } break;
                                    case Keys.OemComma: if (shift) { c = '<'; } else { c = ','; } break;
                                    case Keys.Space: c = ' '; break;
                                }
                                if (c != 'ת')
                                    if (Index == Text.Length)
                                    {
                                        Text += c;
                                        Index++;
                                    }
                                    else
                                        Text = Text.Insert(Index++, c.ToString());
                            if (k == Keys.Back)
                                Text = Text.Remove(Index-- - 1, 1);
                            if (k == Keys.Delete)
                                Text = Text.Remove(Index, 1);
                            if (k == Keys.Right)
                                Index++;
                            if (k == Keys.Left)
                                Index--;
                        }
            }
            catch
            {
            }
            if (Index < 0)
                Index = 0;
            if (Index > Text.Length)
                Index = Text.Length;
            base.Update(time);
        }


        public override float Layer
        {
            get { return 0f; }
        }

        public override object Clone()
        {
            throw new NotImplementedException();
        }
    }
}
